package me.xplabs.magic 
{
	import me.xplabs.gemo.Point2D;
	import me.xplabs.gemo.Point3D;
	import me.xplabs.utils.Utils;
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 * 抛物线技能特效
	 */
	public class ParabolaMagic extends BaseMagicPath 
	{
		private var _gravity:int;
		private var _targetPoint3d:Point3D;
		private var _sourcePoint3d:Point3D;
		private var _curPoint3d:Point3D;
		private var _curSpeed3d:Point3D;
		private var _curSpeed2d:Point2D;
		private var _speedZ:Number;
		public function ParabolaMagic() 
		{
			
		}
		public function initialize(targetPoint2d:Point2D, sourcePoint2d:Point2D, speed:int,gravity:int):void
		{
			_targetPoint2d = targetPoint2d;
			_sourcePoint2d = sourcePoint2d;
			_speed = speed;
			_gravity = gravity;
		}
		override public function startPath():void 
		{
			_targetPoint3d = new Point3D();
			Utils.convert2dTo3d(_targetPoint2d, _targetPoint3d);
			_sourcePoint3d = new Point3D();
			Utils.convert2dTo3d(_sourcePoint2d, _sourcePoint3d);
			
			_distance = _targetPoint3d.distance(_sourcePoint3d);
			_speed *= 5;
			totalTime = _distance / _speed;
			
			curPoint2d = _sourcePoint2d.clone();
			_curPoint3d = _sourcePoint3d.clone();
			
			angle = Math.atan2(_targetPoint3d.y - _sourcePoint3d.y, _targetPoint3d.x - _sourcePoint3d.x);
			_speedZ = totalTime * _gravity;
			_curSpeed3d = new Point3D(_speed * Math.cos(angle), _speed * Math.sin(angle), _speedZ);
			_curSpeed2d = new Point2D();
			Utils.convert3dTo2d(_curSpeed3d, _curSpeed2d);
			_curTime = 0;
			angle = Math.atan2(_curSpeed2d.y, _curSpeed2d.x);
		}
		override public function updateTrack(gap:int):Boolean 
		{
			_curTime += (gap / 1000);
			if (_curTime >= totalTime)
			{
				curPoint2d.setPos(_targetPoint2d);
				return true;
			}
			
			_curPoint3d.z = _speedZ * _curTime -  _gravity * _curTime * _curTime;
			_curPoint3d.x = _sourcePoint3d.x + _curTime * _curSpeed3d.x;
			_curPoint3d.y = _sourcePoint3d.y + _curTime * _curSpeed3d.y;
			
			Utils.convert3dTo2d(_curPoint3d, curPoint2d);
			
			_curSpeed3d.z = _speedZ - _curTime * _gravity * 2;
			Utils.convert3dTo2d(_curSpeed3d, _curSpeed2d);
			angle = Math.atan2(_curSpeed2d.y, _curSpeed2d.x);
			//var tempAngle:Number = angle;
			//angle = 180 * tempAngle / Math.PI;
			return false;
		}
	}

}